﻿/*
* 创建者：LIYINFAI
* 创建时间：2023/10/15 11:40:00
* 描述：
* --------------------------------------
* 修改说明：
* --------------------------------------
*/

using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace CommonLibrary.Forms
{
    /// <summary>
    /// 窗体
    /// </summary>
    [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(System.ComponentModel.Design.IDesigner))]
    public partial class HWFrmWithTitle : HWFrmBase
    {
        private bool mIsTitleShown = true;
        /// <summary>
        /// 是否显示标题栏
        /// </summary>
        [Description("是否显示标题栏"), Category("自定义")]
        public bool IsTitleShown
        {
            get
            {
                return this.mIsTitleShown;
            }
            set
            {
                this.mIsTitleShown = value;
                this.panelTop.Visible = value;
                if (value)
                {
                    this.panelTop.BringToFront();
                }
            }
        }
        private Color mTitleBarBackColor = ColorSettings.BUTTON_BACK_COLOR;
        /// <summary>
        /// 标题栏背景色
        /// </summary>
        [Description("标题栏背景色"), Category("自定义")]
        public Color TitleBarBackColor
        {
            get
            {
                return this.mTitleBarBackColor;
            }
            set
            {
                this.mTitleBarBackColor = value;
                this.panelTop.BackColor = value;
            }
        }
        private Color mTitleBarForeColor = Color.Black;
        /// <summary>
        /// 标题栏前景色
        /// </summary>
        [Description("标题栏前景色"), Category("自定义")]
        public Color TitleBarForeColor
        {
            get
            {
                return this.mTitleBarForeColor;
            }
            set
            {
                this.mTitleBarForeColor = value;
                this.panelTop.ForeColor = value;
            }
        }
        /// <summary>
        /// 窗体标题
        /// </summary>
        [Description("窗体标题"), Category("自定义")]
        public string Title
        {
            get
            {
                return lblTitle.Text;
            }
            set
            {
                lblTitle.Text = value;
            }
        }
        private bool mIsCloseShown = false;
        /// <summary>
        /// IsCloseShown
        /// </summary>
        [Description("是否显示右上角关闭按钮"), Category("自定义")]
        public bool IsCloseShown
        {
            get
            {
                return this.mIsCloseShown;
            }
            set
            {
                this.mIsCloseShown = value;
                this.btnClose.Visible = value;
                if (value)
                {
                    this.btnClose.BringToFront();
                }
            }
        }
        private bool mIsRestoreShown = false;
        /// <summary>
        /// 是否显示右上角还原按钮
        /// </summary>
        [Description("是否显示右上角还原按钮"), Category("自定义")]
        public bool IsRestoreShown
        {
            get
            {
                return this.mIsRestoreShown;
            }
            set
            {
                this.mIsRestoreShown = value;
                this.btnMinMax.Visible = value;
                this.RemoveFormMove(this.lblTitle);
                if (value)
                {
                    this.btnMinMax.BringToFront();
                    this.InitFormMove(this.lblTitle);
                }
            }
        }
        private bool mIsMinShown = false;
        /// <summary>
        /// 是否显示右上角最小化按钮
        /// </summary>
        [Description("是否显示右上角最小化按钮"), Category("自定义")]
        public bool IsMinShown
        {
            get
            {
                return this.mIsMinShown;
            }
            set
            {
                this.mIsMinShown = value;
                this.btnMin.Visible = value;
                if (value)
                {
                    this.btnMin.BringToFront();
                }
            }
        }
        private bool mIsLogoShown = false;
        /// <summary>
        /// 是否显示左上角LOGO
        /// </summary>
        [Description("是否显示左上角LOGO"), Category("自定义")]
        public bool IsLogoShown
        {
            get
            {
                return this.mIsLogoShown;
            }
            set
            {
                this.mIsLogoShown = value;
                this.panelLogo.Visible = value;
                if (value)
                {
                    this.panelLogo.BringToFront();
                }
            }
        }
        private Image mLogo = null;
        /// <summary>
        /// LOGO图像
        /// </summary>
        [Description("LOGO图像"), Category("自定义")]
        public Image Logo
        {
            get
            {
                return this.mLogo;
            }
            set
            {
                this.mLogo = value;
                this.panelLogo.BackgroundImage = value;
            }
        }
        private int mLogoWidth = 120;
        /// <summary>
        /// LOGO图像宽度
        /// </summary>
        [Description("LOGO图像宽度"), Category("自定义")]
        public int LogoWidth
        {
            get
            {
                return this.mLogoWidth;
            }
            set
            {
                this.mLogoWidth = value;
                this.panelLogo.Width = value;
            }
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        public HWFrmWithTitle()
        {
            InitializeComponent();
            InitFormMove(this.lblTitle);
            this.IsLogoShown = true;
            this.IsMinShown = true;
            this.IsRestoreShown = true;
            this.IsCloseShown = true;
            this.labelClose.Font = new Font(FontHelper.GetFont(), 20);
            this.labelClose.Text = FontHelper.CLOSE;
            this.labelMax.Font = new Font(FontHelper.GetFont(), 20);
            this.labelMax.Text = FontHelper.MAX1;
            this.labelMin.Font = new Font(FontHelper.GetFont(), 20);
            this.labelMin.Text = FontHelper.MIN2;
            this.BorderStyleColor = ColorSettings.FORM_BORDER_COLOR;
        }
        private void HWFrmWithTitle_Shown(object sender, EventArgs e)
        {
            if (this.mIsCloseShown)
            {
                this.btnClose.BringToFront();
            }
            if (this.mIsRestoreShown)
            {
                this.btnMinMax.BringToFront();
            }
            if (this.mIsMinShown)
            {
                this.btnMin.BringToFront();
            }
        }
        private void SetMinMaxImage()
        {
            this.labelMax.Text = this.WindowState == FormWindowState.Normal ?
               FontHelper.MAX1 : FontHelper.RESTORE1;
        }
        private void btnMin_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
        private void btnMinMax_Click(object sender, EventArgs e)
        {
            this.WindowState = this.WindowState == FormWindowState.Normal ? FormWindowState.Maximized : FormWindowState.Normal;
            SetMinMaxImage();
        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void btnClose_MouseLeave(object sender, EventArgs e)
        {
            var p = sender as Panel;
            p.BackColor = this.panelTop.BackColor;
        }
        private void btnClose_MouseEnter(object sender, EventArgs e)
        {
            var p = sender as Panel;
            p.BackColor = SystemColors.InactiveCaption;
        }
        private void HWFrmWithTitle_Load(object sender, EventArgs e)
        {
            SetMinMaxImage();
        }
        #region 窗体拖动
        /// <summary>
        /// 
        /// </summary>
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        /// <summary>
        /// 
        /// </summary>
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        /// <summary>
        /// 
        /// </summary>
        public const int WM_SYSCOMMAND = 0x0112;
        /// <summary>
        /// 
        /// </summary>
        public const int SC_MOVE = 0xF010;
        /// <summary>
        /// 
        /// </summary>
        public const int SC_MAXIMIZE = 0xF030;
        /// <summary>
        /// 
        /// </summary>
        public const int SC_RESTORE = 0xF120;
        /// <summary>
        /// 
        /// </summary>
        public const int HTCAPTION = 0x0002;
        #endregion
        /// <summary>
        /// 
        /// </summary>
        protected void InitFormMove(params Control[] cs)
        {
            foreach (Control c in cs)
            {
                if (c != null && !c.IsDisposed)
                {
                    c.MouseDown += C_MouseDown;
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        protected void RemoveFormMove(params Control[] cs)
        {
            foreach (Control c in cs)
            {
                if (c != null && !c.IsDisposed)
                {
                    c.MouseDown -= C_MouseDown;
                }
            }
        }
        private void C_DoubleClick(object sender, EventArgs e)
        {
            ReleaseCapture();
            int wParam = this.WindowState == FormWindowState.Maximized ? SC_RESTORE : SC_MAXIMIZE;
            SendMessage(this.Handle, WM_SYSCOMMAND, wParam, 0);
        }
        void C_MouseDown(object sender, MouseEventArgs e)
        {
            Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")} - {e.Clicks}");
            if (e.Clicks < 2)
            {
                ReleaseCapture();
                SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
            }
            else if (e.Clicks == 2)
            {
                ReleaseCapture();
                int wParam = this.WindowState == FormWindowState.Maximized ? SC_RESTORE : SC_MAXIMIZE;
                SendMessage(this.Handle, WM_SYSCOMMAND, wParam, 0);
                SetMinMaxImage();
            }
        }
        private void labelSize_MouseEnter(object sender, EventArgs e)
        {
            var p = sender as Label;
            p.BackColor = Color.Black;
        }
        private void labelSize_MouseLeave(object sender, EventArgs e)
        {
            var p = sender as Label;
            p.BackColor = this.panelTop.BackColor;
        }
        private void labelMax_Click(object sender, EventArgs e)
        {
            this.WindowState = this.WindowState == FormWindowState.Normal ? FormWindowState.Maximized : FormWindowState.Normal;
            SetMinMaxImage();
        }
        private void labelMin_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
        private void labelClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void HWFrmWithTitle_Resize(object sender, EventArgs e)
        {
            SetMinMaxImage();
            this.Refresh();
        }
    }
}

